package com.kool.kadmin.common.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kool.kadmin.common.bean.*;
import com.kool.kadmin.common.constant.ErrorCode;
import com.kool.kadmin.common.exception.AppException;

import java.util.List;

/**
 * @author luyu
 */
public class ResultUtil {
    /**
     * 返回成功结果
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> ObjectResult<T> success(T t) {
        ObjectResult<T> objectResult = new ObjectResult<T>().setData(t);
        objectResult.setMessage(ErrorCode.SUCCESS.getDesc());
        return objectResult;
    }

    /**
     * 返回成功结果
     *
     * @return
     */
    public static ObjectResult success() {
        ObjectResult objectResult = new ObjectResult();
        objectResult.setMessage(ErrorCode.SUCCESS.getDesc());
        return objectResult;
    }

    /**
     * 返回成功结果
     *
     * @param list
     * @param <T>
     * @return
     */
    public static <T> ListResult<T> success(List<T> list) {
        ListResult<T> listResult = new ListResult<T>().setData(list);
        listResult.setMessage(ErrorCode.SUCCESS.getDesc());
        return listResult;
    }

    /**
     * 返回成功结果
     *
     * @param list
     * @param currentPage
     * @param pageSize
     * @param total
     * @param <T>
     * @return
     */
    public static <T> PageListResult<T> success(List<T> list, int currentPage, int pageSize, int total) {
        PageListResult<T> pageListResult = new PageListResult<T>().setCurrentPage(currentPage).setPageSize(pageSize).setTotal(total).setPageCount((total + pageSize - 1) / pageSize);
        pageListResult.setData(list).setMessage(ErrorCode.SUCCESS.getDesc());
        return pageListResult;
    }

    /**
     * 返回成功结果
     *
     * @param list
     * @param offset
     * @param <T>
     * @return
     */
    public static <T> PageListResult<T> success(List<T> list, String offset) {
        PageListResult<T> pageListResult = new PageListResult<T>().setOffset(offset);
        pageListResult.setData(list).setMessage(ErrorCode.SUCCESS.getDesc());
        return pageListResult;
    }

    /**
     * 返回失败结果
     *
     * @param errorCode
     * @param args
     * @return
     */
    @Deprecated
    public static Result error(ErrorCode errorCode, Object... args) {
        String msg = String.format(errorCode.getDesc(), args);
        return new Result().setCode(errorCode.getCode()).setMessage(msg);
    }

    /**
     * 返回失败结果
     *
     * @param errorCode
     * @param errors
     * @param args
     * @return
     */
    @Deprecated
    public static Result error(ErrorCode errorCode, List<ErrorVo> errors, Object... args) {
        String msg = String.format(errorCode.getDesc(), args);
        return new Result().setCode(errorCode.getCode()).setMessage(msg).setErrors(errors);
    }

    /**
     * 解包数据
     *
     * @param response
     * @return
     */
    public static String unpackData(String response) {
        if (StringUtil.isEmpty(response)) {
            return null;
        }
        JSONObject result = JSON.parseObject(response);
        Integer code = result.getInteger("code");
        String msg = result.getString("message");
        String data = result.getString("data");
        if (code == null) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, "响应体格式错误：code为空");
        }
        if (code != 0) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, "外部服务返回错误：" + msg);
        }
        return data;
    }
}
